const app = getApp();
Page({
  data: {
    picker: ['按日期导出', '按账本导出'],
    by_index:0,
    book_index:0,
    book_list:[],
    start_time: '2020-01-01',
    end_time: '2020-01-01',
    output_type:'', //邮箱导出还是excel导出
    output_url:'', //导出外链
    book_id:'', //账本
    email:'', //输入的邮箱
  },
  onLoad: function (options) {
    let _this = this,
     output_type = options.import_type;
    _this.setData({
      output_type:'excel'
    })
    _this.getBookList();
  },

  //邮箱输入
  input_email:function(e){
    let _this = this,
     input_value = e.detail.value;
    _this.setData({
      email:input_value
    })
  },

  //获取账本
  getBookList:function(){
    let _this = this;
    app._post('api/member/getUserBookList', {}, function(result) {
      if(result.code == 200){
        _this.setData({
          book_list:result.data,
          book_id:result.data[0].book_id
        })
      }else{
        wx.navigateTo({
          url: '/pages/auth/auth'
        })
      }
    });
  },

  //复制结果
  copyText: function (e) {
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },

  //确认导出
  confirm_output:function(){
    let _this = this,
     start_time = _this.data.start_time,
     end_time = _this.data.end_time,
     output_type = _this.data.output_type,
     by_index = _this.data.by_index,
     book_id = _this.data.book_id,
     email = _this.data.email;

    var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");

    if(output_type == 'email' && email == ''){
      wx.showToast({
        title: '邮箱不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    if(output_type == 'email' && !reg.test(email)){
      wx.showToast({
        title: '邮箱格式不正确',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    app._post('api/member/import_bill', {
      start_time:start_time,
      end_time:end_time,
      output_type:output_type,
      book_id:book_id,
      by_index:by_index,
      email:email,
    }, function(result) {
      if(output_type == 'excel'){
        _this.setData({
          output_url:result.data.url
        })
        wx.showToast({
          title: result.msg,
          icon: 'none',
          duration: 2000
        })
      }else{
        if(result.code == 200){
          var icon = 'success';
        }else{
          var icon = 'none';
        }
        wx.showToast({
          title: result.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },

  //选择导出依据
  PickerChange(e) {
    this.setData({
      by_index: e.detail.value
    })
  },

  //账本选择
  BookChange(e){
    let _this = this,
     book_list = _this.data.book_list;
    _this.setData({
      book_index: e.detail.value,
      book_id:book_list[e.detail.value].book_id
    })
  },

  //开始时间选择器
  DateChange(e) {
    this.setData({
      start_time: e.detail.value
    })
  },

  //结束时间选择器
  DateChange1(e) {
    this.setData({
      end_time: e.detail.value
    })
  },

});
